package com.jrapider.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jrapider.om.IdGetter;
import com.jrapider.om.JSONResult;
import com.jrapider.om.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;


@Log4j2
public abstract class BaseCrudController<S extends ServiceImpl, T extends IdGetter> implements InitializingBean {
    protected String pagePath;
    @Autowired
    protected S service;

    public void buildCondition(QueryWrapper<T> qw, T condition, String filter) {
        qw.setEntity(condition);
    }

    @Override
    public void afterPropertiesSet() {
        RequestMapping anno = this.getClass().getAnnotation(RequestMapping.class);
        pagePath = anno.value()[0];
    }

    @ApiOperation(value = "查询页")
    @RequestMapping(value = "/page/query", method = RequestMethod.GET)
    public String query() {
        return pagePath + "/query";
    }

    @ApiOperation(value = "编辑页")
    @RequestMapping(value = "/page/edit", method = RequestMethod.GET)
    public String edit(Long id, Model model) {
        Object data = service.getById(id);
        model.addAttribute("data", data == null ? new Object() : data);
        return pagePath + "/edit";
    }

    @ApiOperation(value = "查看页")
    @RequestMapping(value = "/page/view", method = RequestMethod.GET)
    public String view(Long id, Model model) {
        Object data = service.getById(id);
        model.addAttribute("data", data == null ? new Object() : data);
        return pagePath + "/view";
    }

    @ApiOperation(value = "查询", httpMethod = "POST")
    @RequestMapping(value = "/data/load")
    @ResponseBody
    public JSONObject load(T condition, @RequestParam(required = false) String filter, PageInfo pageInfo) {
        QueryWrapper<T> qw = new QueryWrapper<>();
        buildCondition(qw, condition, filter);
        IPage<Object> pageResult = service.page(pageInfo.conventToMybatisPage(Object.class), qw);
        return JSONResult.newBuilder()
                .setRows(pageResult.getRecords())
                .setCount(pageResult.getTotal())
                .success("查询成功")
                .build();
    }

    @ApiOperation(value = "查看")
    @RequestMapping(value = "/data/get", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject load(Long id) {
        Object data = service.getById(id);
        if (data == null) {
            return JSONResult.newBuilder().fail("数据不存在").build();
        } else {
            return JSONResult.newBuilder().success("查询成功").setData(data).build();
        }
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/data/del", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject del(Long[] id) {
        if (service.removeByIds(Arrays.asList(id))) {
            return JSONResult.newBuilder().success("删除成功").build();
        } else {
            return JSONResult.newBuilder().fail("删除失败").build();
        }
    }

    @ApiOperation(value = "保存")
    @RequestMapping(value = "/data/save", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject save(T data) {
        if (service.saveOrUpdate(data)) {
            return JSONResult.newBuilder().success("保存成功").setData(data.getId()).build();
        } else {
            return JSONResult.newBuilder().fail("保存失败").build();
        }
    }


}
